#include <cstdio>

const int maxn = 150001;
int rank[maxn], par[maxn];

void init(int n);
int find(int x);
void unite(int x, int y);

int main()
{
	int n, k, ans = 0;
	scanf("%d%d", &n, &k);
	init(3*n);
	for (int i = 0; i < k; ++i) {
		int d, x, y;
		scanf("%d%d%d", &d, &x, &y);
		if (x > n || y > n) ++ans;
		else
			if (d == 1) {
				int tmp = find(x);
				if (tmp == find(y + n) || tmp == find(y + 2*n)) ++ans;
				else {
					unite(x, y);
					unite(x + n, y + n);
					unite(x + 2*n, y + 2*n);
				}
			} else {
				int tmp = find(x);
				if (tmp == find(y) || tmp == find(y + 2*n)) ++ans;
				else {
					unite(x, y + n);
					unite(x + n, y + 2*n);
					unite(x + 2*n, y);
				}
			}
	}
	printf("%d\n", ans);
	return 0;
}

void init(int n)
{
	for (int i = 1; i <= n; ++i) par[i] = i; // rank already 0
}

int find(int x)
{
	if (par[x] == x) return x;
	else			 return (par[x] = find(par[x]));
}

void unite(int x, int y)
{
	x = find(x);
	y = find(y);
	if (x == y) return;
	if (rank[x] < rank[y]) par[x] = y;
	else {
		par[y] = x;
		if (rank[x] == rank[y]) ++rank[x];
	}
}
